package com.ithei.多线程第二天.并发工具类.Hashtable;

import java.util.concurrent.Semaphore;

public class DemoSemaphore红绿灯 {
    public static void main(String[] args) {
        Semaphore sem=new Semaphore(2);
        for (int i = 1; i <= 100; i++) {
            new Thread(()->{
                try {
                    sem.acquire();
                    System.out.println(Thread.currentThread().getName()+"获得通行证..........");
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().getName()+"归还通行证...........");
                    sem.release();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"第"+i+"辆车").start();
        }





    }
}
